package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.Album;
import com.example.entity.Myalbum;
import com.example.mapper.AlbumMapper;
import com.example.mapper.MyalbumMapper;
import com.example.utils.BisException;
import com.example.utils.ResultData;
import com.example.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * The type Album service.
 */
@Service
public class AlbumService {
    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private MyalbumMapper myalbumMapper;

    /**
     * Add myalbum result data.
     *
     * @param file    the file
     * @param myalbum the myalbum
     * @return the result data
     */
    public ResultData addMyalbum(MultipartFile file, Myalbum myalbum) {
        if (!file.isEmpty()) {
            //获取文件名
            String fileName = file.getOriginalFilename();
            //获取文件后缀
            String suffixName = fileName.substring(fileName.indexOf("."));
            //重新生成文件名
            String md5Name = DigestUtils.md5DigestAsHex(fileName.getBytes());
            String filename = md5Name + suffixName;
            //指定本地存入路径
            String filePath = "/www/admin/www.xinggaoya.com_80/wwwroot/blog/album/";
            String LinuxPath = filePath + filename;
            try {
                file.transferTo(new File(LinuxPath));
                Runtime.getRuntime().exec("chmod 777 -R " + LinuxPath);
                String MysqlName = "http://www.xinggaoya.com/blog/album/" + filename;
                myalbum.setMyalbumUrl(MysqlName);
            } catch (IOException e) {
                e.printStackTrace();
                return ResultUtils.error("文件上传失败", null);
            }
        } else {
            throw new BisException("文件是空的");
        }
        int res = myalbumMapper.insert(myalbum);
        if (res > 0) {
            return ResultUtils.sucuss("图片上传成功", res);
        } else {
            return ResultUtils.error("上传错误！", null);
        }

    }

    /**
     * Gets album.
     *
     * @param myalbum the myalbum
     * @return the album
     */
    public ResultData getmyAlbum(Myalbum myalbum) {
        QueryWrapper<Myalbum> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", myalbum.getUserName());
        List<Myalbum> res = myalbumMapper.selectList(queryWrapper);
        if (!res.isEmpty()) {
            return ResultUtils.sucuss("查询成功", res);
        } else {
            return ResultUtils.error("失败", null);
        }

    }

    /**
     * Add album result data.
     *
     * @param album the album
     * @return the result data
     */
    public ResultData addAlbum(Album album) {
        if (!album.getUserName().equals("") && !album.getAlbumContent().equals("")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            album.setAlbumDate(sdf.format(new Date()));
            int res = albumMapper.insert(album);
            return ResultUtils.sucuss("相册新建成功", res);
        } else {
            return ResultUtils.error("相册新建失败", null);
        }

    }

    /**
     * Gets album.
     *
     * @param album the album
     * @return the album
     */
    public ResultData getAlbum(Album album) {
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", album.getUserName()).orderByDesc("album_date");
        List<Album> res = albumMapper.selectList(queryWrapper);
        if (!res.isEmpty()) {
            return ResultUtils.sucuss("查询成功", res);
        } else {
            return ResultUtils.error("失败", null);
        }
    }

    /**
     * Delete album result data.
     *
     * @param album the album
     * @return the result data
     */
    public ResultData DeleteAlbum(Album album) {

        int result = albumMapper.deleteById(album.getAlbumId());
        if (result > 0) {
            QueryWrapper<Myalbum> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("album_id", album.getAlbumId());
            int res = myalbumMapper.delete(queryWrapper);
            if (res > 0) {
                return ResultUtils.sucuss("全部删除成功！", result);
            } else {
                return ResultUtils.sucuss("无照片，相册删除成功", result);
            }
        } else {
            return ResultUtils.error("删除失败！", null);
        }
    }

    /**
     * Delete my album result data.
     *
     * @param myalbum the myalbum
     * @return the result data
     */
    public ResultData DeleteMyAlbum(Myalbum myalbum) {
        int result = myalbumMapper.deleteById(myalbum.getMyalbumId());
        if (result > 0) {
            return ResultUtils.sucuss("删除成功！", result);
        } else {
            return ResultUtils.error("删除失败！", null);
        }
    }

}
